package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2024-01-14
 * Time: 10:25
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack(int[] elem) {
        this.elem = new int[10];
    }
    public void push(int data) {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    public int pop() throws EmptyException {
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        return this.elem[--this.usedSize];
    }
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    public int peek() throws EmptyException {
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        return this.elem[this.usedSize - 1];
    }
}
